package com.yjd.job.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.yjd.comm.base.dao.IBaseDao;
import com.yjd.comm.base.model.PagerModel;
import com.yjd.comm.base.service.IPager;
import com.yjd.comm.base.service.impl.BaseServiceImpl;
import com.yjd.comm.util.PagerUtil;
import com.yjd.job.dao.IJobLogDao;
import com.yjd.job.model.JobLogModel;

/**
 * <b>description</b>：定时器日志服务类 <br>
 * <b>time</b>：2014-11-10下午1:37:59 <br>
 * <b>author</b>： ready likun_557@163.com
 */
@Service("jobLogService")
public class JobLogServiceImpl extends BaseServiceImpl implements
		IJobLogService {

	/**
	 * 新增
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public JobLogModel insert(JobLogModel model) throws Exception {
		return this.jobLogDao.insertModel(model, true);
	}

	/**
	 * 根据job的日志编号删除job日志
	 * 
	 * @param idJobLog
	 *            job日志编号
	 * @return
	 * @throws Exception
	 */
	public int delete(Long idJobLog) throws Exception {
		int result = -1;
		if (idJobLog == null) {
			Map<Object, Object> paramMap = new HashMap<Object, Object>();
			paramMap.put("idJobLog", idJobLog);
			result = this.deleteByMap(paramMap);
		}
		return result;
	}

	/**
	 * 根据Map中的参数删除对象
	 * 
	 * @param paramMap
	 * @return
	 * @throws Exception
	 */
	public int deleteByMap(Map<Object, Object> paramMap) throws Exception {
		return this.jobLogDao.deleteModel(paramMap, true);
	}

	/**
	 * 更新job日志
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public int update(JobLogModel model) throws Exception {
		return this.jobLogDao.updateModel(model, true);
	}

	/**
	 * job执行日志数据访问对象
	 */
	@Resource
	private IJobLogDao jobLogDao;

	@Override
	public IBaseDao getBaseDao() {
		return this.jobLogDao;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public PagerModel getFailedJobLogPage(Map paramMap) throws Exception
	{
		return PagerUtil.getPagerModel(new IPager() {

			@Override
			public long getPageCount(Map<Object, Object> paramMap) throws Exception
			{
				return jobLogDao.getFailedJobListCount(paramMap, false);
			}

			@Override
			public List getPageList(Map<Object, Object> paramMap) throws Exception
			{
				return jobLogDao.getFailedJobList(paramMap, false);
			}
			
		}, paramMap, jobLogDao.getDbtype());
	}

}
